In [1]:
import twitter
# Go to http://dev.twitter.com/apps/new to create an app and get values
# for these credentials, which you'll need to provide in place of these
# empty string values that are defined as placeholders.
# See https://dev.twitter.com/docs/auth/oauth for more information
# on Twitter's OAuth implementation.
CONSUMER_KEY = 'Tx8GozuzaraJO89LypsTqFUNM'
CONSUMER_SECRET ='uC0KPaRZjGY0qoNJAxBQb0jbb3Us4DXJVKFCP1qtSfjFgbRvd0'
OAUTH_TOKEN = '571213367-BC7MYnWpLbvy2AYhvqCu6Fh1EIeFasPxo0Y8uBEn'
OAUTH_TOKEN_SECRET = 'KA9GddiLbVwummLoSGmDYMNQFVaR3P4YiGo7Zs6D5e7Qz'
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
# Nothing to see by displaying twitter_api except that it's now a
# defined variable
print twitter_api
In [2]:
# The Yahoo! Where On Earth ID for the entire world is 1.
# See https://dev.twitter.com/docs/api/1.1/get/trends/place and
# http://developer.yahoo.com/geo/geoplanet/
WORLD_WOE_ID = 1
US_WOE_ID = 23424977
# Prefix ID with the underscore for query string parameterization.
# Without the underscore, the twitter package appends the ID value
# to the URL itself as a special case keyword argument.
world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
us_trends = twitter_api.trends.place(_id=US_WOE_ID)
In [3]:
import json
import pymongo
In [4]:
# Based upon example 9-7 in *required reading*
# Mining the Soocial Web, Chapter 9
# Connects to the MongoDB server running on
# localhost:27017 by default
client = pymongo.MongoClient()
# Get a reference to a particular database
db = client['twitter']
# Reference a particular collection in the database
coll = db['us_trends']
# Clear any old data out of the database
# **Only for Demonstration**
coll.drop()
# Perform a bulk insert and return the IDs
_ = coll.insert(us_trends[0]['trends'])
In [5]:
# Connects to the MongoDB server running on
# localhost:27017 by default
client = pymongo.MongoClient()
# Get a reference to a particular database
db = client['twitter']
# Reference a particular collection in the database
coll = db['us_trends']
In [6]:
cursor = coll.find({})
In [7]:
for trend in cursor:
print trend['name'], trend['tweet_volume']
In [8]:
# Do a search! See
# https://docs.mongodb.org/getting-started/python/query/
# and
# https://docs.mongodb.org/manual/tutorial/query-documents/
# for details.
cursor = coll.find({'tweet_volume': {'$gt': 30000} })
In [9]:
for trend in cursor:
print trend['name'], trend['tweet_volume']
In [10]:
# Do a search! See
# https://docs.mongodb.org/getting-started/python/query/
# and
# https://docs.mongodb.org/manual/tutorial/query-documents/
# for details.
cursor = coll.find({'$or': [ {'tweet_volume': {'$gt': 30000}} , {'name': {'$regex': '.*love.*', '$options': 'i' }} ] })
In [11]:
for trend in cursor:
print trend['name'], trend['tweet_volume']
In [ ]:
In [ ]: